#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SNMP;
use FileHandle;
use Storable qw(store);
use OIDS;
use Cwd;

my $cur_dir = getcwd();
require "$cur_dir/common_snmp.pl";

BEGIN { our $start_run = time(); }
my ($timestamp, $ERR);
if (@ARGV >= 1) {
	$timestamp = $ARGV[0];
} else {
	print "ERROR: no argument passed";
	$ERR .= "ERROR: no argument passed\n";
	exit 1;
}

# global variables
my $duration = 10;
my $data_path = $cur_dir;
$data_path =~ s/cgi-bin/wMOS/;
my @numeric_oid_arr;
my %ap_list = ();
my %client_list = ();

my ($client_ip, $ap_ip, $client_ap, $client_mac_dec, $client_mac_hex, $APSlotId_list, $client_APSlotId, $APs_current_channel);

# host info
my $ip = '11.37.200.5';
my $host = '10.89.234.220';
my $community = 'Altest';

for my $key ( keys %Numeric_Oid_arr ) {
        push (@numeric_oid_arr, $Numeric_Oid_arr{$key});
}

# create SNMP session 
my ($error, $session) = snmp_connect($host, $community);

if (!$session) {
	print "ERROR:$error";
    exit 1;
} else {
	# collect clients info from the host (WCS)
	$client_ip = $session->get_table(-baseoid => $OID_grp_bsnMobileStationIpAddress);
	$client_ap = $session->get_table(-baseoid =>  $OID_grp_bsnMobileStationAPMacAddr);
	$ap_ip = $session->get_table(-baseoid => $OID_grp_bsnApIpAddress);
	$APSlotId_list = $session->get_table(-baseoid => $OID_grp_bsnMobileStationAPIfSlotId);
	$APs_current_channel = $session->get_table(-baseoid => $OID_grp_bsnAPIfPhyChannelNumber);
}

$session->close();
# tidy up the raw data from WCS
foreach my $oid (keys %$client_ip) {

	$client_mac_dec = $oid;
	$client_mac_dec =~ s/$OID_grp_bsnMobileStationIpAddress\.//;

	$client_mac_hex = format_mac_decimal($client_mac_dec);
	$client_list{$client_ip->{$oid}}{'mac_hex'} = $client_mac_hex;
	my $AP_mac_hex = format_mac_hex($client_ap->{"$OID_grp_bsnMobileStationAPMacAddr.$client_mac_dec"});
	my $AP_mac_dec = mac_hex_decimal($AP_mac_hex);
	$client_list{$client_ip->{$oid}}{'AP_hex'} = $AP_mac_hex;
	$client_list{$client_ip->{$oid}}{'AP_IP'} = $ap_ip->{"$OID_grp_bsnApIpAddress.$AP_mac_dec"};
	$client_list{$client_ip->{$oid}}{'APSlotId'} = $APSlotId_list->{"$OID_grp_bsnMobileStationAPIfSlotId.$client_mac_dec"};
	$client_list{$client_ip->{$oid}}{'channel'} = $APs_current_channel->{"$OID_grp_bsnAPIfPhyChannelNumber.$AP_mac_dec.$client_list{$client_ip->{$oid}}{'APSlotId'}"};
	
	#client and assoicated AP info logging
	my $fh = FileHandle->new();
	my $filename = $data_path . "/" . $client_mac_hex . "-" . $timestamp . ".log";
	$filename =~ s/:/-/g;
	open($fh, "> $filename");
	print $fh "Client INFO\tMAC address:$client_mac_hex,IP_Address:$client_ip->{$oid},Channel:$client_list{$client_ip->{$oid}}{'channel'}\n";	
	print $fh "Assoicated AP INFO\tMAC_address:$client_list{$client_ip->{$oid}}{'AP_hex'},IP_address:$client_list{$client_ip->{$oid}}{'AP_IP'},AP_Slot_Id:$client_list{$client_ip->{$oid}}{'APSlotId'}\n";
	close $fh;
	print "$filename\n";
}
store(\%client_list, $timestamp) or die "ERROR: Can't dump \%client_list!\n";

